<script setup name="person">
import useUserStore from "@/store/modules/user.js";
import {addBorrow, getBorrow, listBorrow, updateBorrow} from "@/api/system/borrow.js";
import {updateDevices} from "@/api/system/devices.js";

const { proxy } = getCurrentInstance();

let userStore = useUserStore();
let user_id = userStore.id
let borrowList = ref([]);
const open = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
let loading = ref(true)
const getList = ()=>{
  listBorrow({stuId: user_id,pageNum:1,pageSize:1000}).then(res => {
    borrowList.value = res.rows
    loading.value = false
  })
}
listBorrow({stuId: user_id,pageNum:1,pageSize:1000}).then(res => {
  borrowList.value = res.rows
  loading.value = false
})
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 1000,
    stuId: null,
    stuName: null,
    startTime: null,
    endTime: null,
    status: null,
    deviceId: null,
    deviceName: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);
function reset() {
  form.value = {
    id: null,
    stuId: null,
    stuName: null,
    startTime: null,
    endTime: null,
    status: null,
    deviceId: null,
    deviceName: null
  };
  proxy.resetForm("borrowRef");
}

function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBorrow(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备借还";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["borrowRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBorrow(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
          if(form.value.status === '已归还'){

          updateDevices({id: form.value.deviceId, status: '可借'}).then(response => {
            // proxy.$modal.msgSuccess("修改成功");
          });
          }
        });
      } else {
        addBorrow(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

</script>

<template>
<div>
  <el-table v-loading="loading" :data="borrowList" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column label="ID" align="center" prop="id" />
    <el-table-column label="学生ID" align="center" prop="stuId" />
    <el-table-column label="学生姓名" align="center" prop="stuName" />
    <el-table-column label="开始时间" align="center" prop="startTime" width="180">
      <template #default="scope">
        <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
    <el-table-column label="结束时间" align="center" prop="endTime" width="180">
      <template #default="scope">
        <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
    <el-table-column label="状态" align="center" prop="status" >
      <template #default="scope">
        <el-tag v-if="scope.row.status === '未归还'" type="danger">{{ scope.row.status }}</el-tag>
        <el-tag v-else type="success">{{ scope.row.status }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="设备ID" align="center" prop="deviceId" />
    <el-table-column label="设备名称" align="center" prop="deviceName" />
    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      <template #default="scope">
        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog :title="title" v-model="open" width="500px" append-to-body>
    <el-form ref="borrowRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="form.startTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="form.endTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input disabled v-model="form.deviceId" placeholder="请输入设备ID" />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input disabled v-model="form.deviceName" placeholder="请输入设备名称" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="未归还" value="未归还" />
            <el-option label="已归还" value="已归还" />
          </el-select>

      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</div>
</template>

<style scoped lang="scss">

</style>